﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using MenesDroid;
using XNA_Game_Engine.GUI;
using XNA_Game_Engine.Sounds;
using XNA_Game_Engine.Events;

namespace XNA_Game_Engine.Canvas
{
    class LevelCompleteFrame : Frame
    {

        Texture2D levelCompleteSign;
        Texture2D highestScoreSign;
        Texture2D newHighScoreSign;
        Texture2D scoreSign;

        SpriteFont fonte;
        SpriteFont fonte2;
        int oldScore, newScore;
        bool highScore;


        public LevelCompleteFrame(Game game, int x, int y, int frameWidth, int frameHeight)
            : base( game, x, y, frameWidth, frameHeight, false )
        {
            levelCompleteSign = null;
        }  


        public override void LoadContent(ContentManager content)
        {
            levelCompleteSign = content.Load<Texture2D>(@"Texts\level_complete");
            highestScoreSign = content.Load<Texture2D>(@"Texts\highestScore");
            newHighScoreSign = content.Load<Texture2D>(@"Texts\newHigh");
            scoreSign = content.Load<Texture2D>(@"Texts\score");

            Buttons.Add(new clsButton(content.Load<Texture2D>(@"Buttons\main_menu_button"),
                                     content.Load<Texture2D>(@"Buttons\main_menu_button"),
                                     new Vector2(X + 30, FrameHeight ), new Vector2(FrameWidth / 4, 75),
                                     ActionsHandler.MainMenuButtonEvent ));

            Buttons.Add(new clsButton(content.Load<Texture2D>(@"Buttons\retry_button"),
                                     content.Load<Texture2D>(@"Buttons\retry_button"),
                                     new Vector2(X + FrameWidth / 4 + 30, FrameHeight), new Vector2(FrameWidth / 4, 75),
                                     ActionsHandler.ReLoadLevel ));

            Buttons.Add(new clsButton(content.Load<Texture2D>(@"Buttons\next_button"),
                                     content.Load<Texture2D>(@"Buttons\next_button"),
                                     new Vector2(X + ((FrameWidth / 4) * 2) + 30, FrameHeight), new Vector2(FrameWidth / 4, 75),
                                     ActionsHandler.LoadNextLevel));
                     
            base.LoadContent(content);

            fonte = content.Load<SpriteFont>(@"Score");
            fonte2 = content.Load<SpriteFont>(@"Score2"); // Fonte menor para o highest score.
        }



        public override void Update(GameTime gameTime)
        {
            if (!_enabled) // Michel? eu coloquei esse IF. Fica updatando direto sem necessidade.
                return;

            ActionsHandler.UnlockLevel();
            base.Update(gameTime);
        }



        public override void Render(SpriteBatch renderTarget)
        {
            if (!_enabled)
                return;

            SoundController.playSound("levelCompleted");
            
            base.Render(renderTarget);

            renderScore(renderTarget);

            renderTarget.Draw(levelCompleteSign, new Rectangle(X + 200, Y + 50, FrameWidth - 400, 100), Color.White);            
        }



        public void renderScore(SpriteBatch renderTarget)
        {

            if (oldScore == 0) // PRIMEIRA JOGADA NA FASE
            {
                renderTarget.Draw(newHighScoreSign, new Rectangle(X + FrameWidth / 2 - 350, Y + FrameHeight / 2 - 20, newHighScoreSign.Width, newHighScoreSign.Height), Color.White);
                renderTarget.DrawString(fonte, newScore.ToString(), new Vector2(X + FrameWidth / 2 + 100, Y + FrameHeight / 2), Color.White);
            }
            else
            { // RENDER HIGHSCORE DA FASE
                renderTarget.Draw(highestScoreSign, new Rectangle(X + FrameWidth / 2 - 280, Y + FrameHeight / 2 - 50, highestScoreSign.Width - 120, highestScoreSign.Height - 30), Color.White);
                renderTarget.DrawString(fonte2, oldScore.ToString(), new Vector2(X + FrameWidth / 2 + 110 , Y + FrameHeight / 2 - 45), Color.White);

                if (highScore == true) // SE JOGADOR FEZ PONTUAÇAO MAIOR QUE O HIGHSCORE DA FASE
                {
                    renderTarget.Draw(newHighScoreSign, new Rectangle(X + FrameWidth / 2 - 350, Y + FrameHeight / 2 + 30, newHighScoreSign.Width, newHighScoreSign.Height), Color.White);
                    renderTarget.DrawString(fonte, newScore.ToString(), new Vector2(X + FrameWidth / 2 + 100, Y + FrameHeight / 2 + 50), Color.White);
                }
                else
                { // SE NAO FEZ MAIS QUE O HIGHSCORE.
                    renderTarget.Draw(scoreSign, new Rectangle(X + FrameWidth / 2 - 180 , Y + FrameHeight / 2 + 50, scoreSign.Width, scoreSign.Height), Color.White);
                    renderTarget.DrawString(fonte, newScore.ToString(), new Vector2(X + FrameWidth / 2, Y + FrameHeight / 2 + 30), Color.White);
                }
            }
        }



        public void setScore(float oldScore, float newScore, bool highScore)
        {
            this.oldScore = (int)oldScore;
            this.newScore = (int)newScore;
            this.highScore = highScore;
        }
    }
}
